import ourcity.core.models as cm
import ourcity.news.models as nm
import ourcity.reps.models as rm

from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext
from django.template.loader import get_template
from ourcity.core.models import Profile
from ourcity.core.forms import ContactForm
from ourcity.reps import lookup


def index(request):
    context = RequestContext(request)
    if request.user.is_authenticated():
        if request.GET.get('a'):
            news = nm.News.objects.active()
            current = 'all'
        else:
            try:
                districts = request.user.get_profile().districts.all()
            except Profile.DoesNotExist:
                return HttpResponseRedirect(reverse('profiles_create_profile'))
            reps = rm.Representative.objects.active().filter(
                    district__in=districts)
            news = nm.News.objects.active().filter(reps__in=reps).distinct()
            current = 'related'
            if request.GET.get('r'):
                news = news.filter(type__name='Report')
                current = 'reports'
            elif request.GET.get('p'):
                news = news.filter(type__name='Post')
                current = 'posts'
        context['news'] = news[:5]
        context['current'] = current
        template = get_template('index-loggedin.html')
    else:
        context['featured_content'] = cm.FeaturedContent.objects.active()
        context['featured_report'] = nm.News.objects.reports()[0]
        context['from_our_blog'] = nm.News.objects.posts()[0]
        context['featured_comment'] = cm.FeaturedComment.objects.filter(
            active=True)[0].comment
        context['featured_issue'] = cm.FeaturedIssue.objects.filter(
            active=True)[0].issue
        template = get_template('index.html')
    return HttpResponse(template.render(context))

def about(request):
    context = RequestContext(request)
    template = get_template('about.html')
    return HttpResponse(template.render(context))

def about_site(request):
    context = RequestContext(request)
    template = get_template('about_site.html')
    return HttpResponse(template.render(context))

def faqs(request):
    context = RequestContext(request)
    template = get_template('faqs.html')
    return HttpResponse(template.render(context))

def help(request):
    context = RequestContext(request)
    template = get_template('help.html')
    return HttpResponse(template.render(context))
    
def community(request):
    context = RequestContext(request)
    template = get_template('community.html')
    return HttpResponse(template.render(context))    

def contact(request):
    context = RequestContext(request)
    if request.GET.get('success'):
        context['success'] = True
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('contact') + '?success=yes')
    else:
        form = ContactForm()
    context['form'] = form
    template = get_template('contact.html')
    return HttpResponse(template.render(context))
